//
//  SimBottomLineSegmentView.m
//  TKnowBox
//
//  Created by Jones on 15/12/30.
//  Copyright © 2015年 knowin. All rights reserved.
//

#import "SimBottomLineSegmentView.h"
#import <SimPods/SimLineView.h>
#import <SimPods/SimButton.h>
#import <SimPods/UIView+SimAdditions.h>

@interface SimBottomLineSegmentView()
{
    NSMutableArray *_items;
    UIView *_bottomLine;
    CGFloat _btnWidth;
}

@end

@implementation SimBottomLineSegmentView

- (instancetype)initWithItems:(NSArray *)items
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _items = [NSMutableArray arrayWithArray:items];
        _normalColor = HEXRGBCOLOR(0x5e6166);
        _selectedColor = HEXRGBCOLOR(0x64dcdc);
        _titleFont = [UIFont systemFontOfSize:14];
    }
    return self;
}

- (void)willMoveToWindow:(UIWindow *)newWindow
{
    if (newWindow != nil && _bottomLine == nil) {
        [self layoutItems];
    }
}

- (void)insertItem:(NSString *)item atIndex:(NSInteger)index
{
    [_items insertObjects:@[item] atIndexes:[NSIndexSet indexSetWithIndex:index]];
    if (index <= _selectedSegmentIndex) {
        _selectedSegmentIndex = MIN(_items.count-1, _selectedSegmentIndex+1);
    }
    if (self.window != nil) {
        [self layoutItems];
    }
}

- (void)removeItemAtIndex:(NSInteger)index
{
    [_items removeObjectAtIndex:index];
    if (index <= _selectedSegmentIndex) {
        _selectedSegmentIndex = MAX(0, _selectedSegmentIndex-1);
    }
    if (self.window != nil) {
        [self layoutItems];
    }
}

- (void)layoutItems
{
    if (_items.count == 0) {
        return;
    }
    [self removeAllSubviews];
    
    _btnWidth = self.width / _items.count;
    
    NSMutableArray *btns = [NSMutableArray array];
    for (int i = 0; i < _items.count; i++) {
        SimButton *btn = [[SimButton alloc] initWithFrame:CGRectMake(i * _btnWidth, 0, _btnWidth, self.height)];
        btn.tag = i;
        [btn setTitle:_items[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:_normalColor forState:UIControlStateNormal];
        [btn setTitleColor:_selectedColor forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(clickOnbtn:) forControlEvents:UIControlEventTouchUpInside];
        if (_setupItemAtIndexBlock) {
            _setupItemAtIndexBlock(btn, i);
        }
        [btns addObject:btn];
        [self addSubview:btn];
    }
    _itemBtns = [NSArray arrayWithArray:btns];
    
    _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 2, _btnWidth, 2)];
    _bottomLine.backgroundColor = _selectedColor;
    [self addSubview:_bottomLine];
    
    SimLineView *lineView = [[SimLineView alloc] initWithWidth:self.width];
    lineView.bottom = self.height;
    [self addSubview:lineView];
    
    [self updateSelectStatus];
}

- (void)updateSelectStatus
{
    for (int i = 0; i < _itemBtns.count; i++) {
        UIButton *btn = _itemBtns[i];
        btn.selected = (i == _selectedSegmentIndex);
    }
    
    [UIView animateWithDuration:0.1 animations:^{
        _bottomLine.left = _selectedSegmentIndex * _btnWidth;
    }];
}

- (void)clickOnbtn:(UIButton *)btn
{
    self.selectedSegmentIndex = btn.tag;
}

- (void)setSelectedSegmentIndex:(NSInteger)index
{
    if (_selectedSegmentIndex != index) {
        _selectedSegmentIndex = index;
        if (self.superview != nil) {
            [self updateSelectStatus];
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }
    }
}

@end
